package com.jhecis.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jhecis.dto.CommonResult;
import com.jhecis.mappers.InterfaceToolMapper;
import com.jhecis.po.InterfaceToolEntity;
import com.jhecis.service.impl.InterfaceToolImpl;
import com.jhecis.utils.RandomGUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InterfaceToolService {

    @Resource
    private InterfaceToolMapper interfaceToolMapper;

    @Resource
    private InterfaceToolImpl interfaceTool;
    public List<InterfaceToolEntity> getInterfaceTool()
    {
        QueryWrapper<InterfaceToolEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("invalidflag",0);
        return interfaceToolMapper.selectList(queryWrapper);
    }

    public InterfaceToolEntity getInterfaceToolByCode(String interfaceCode)
    {
//        List<InterfaceToolEntity> list= interfaceTool.getList(interfaceCode);
//        if (list!=null && list.size()>0) {
//            return list.get(0);
//        }

        QueryWrapper<InterfaceToolEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("interfacecode",interfaceCode);
        List<InterfaceToolEntity> list= interfaceToolMapper.selectList(queryWrapper);
        if (list!=null && list.size()>0) {
            return list.get(0);
        }
        return null;
    }
    @Transactional
    public CommonResult saveInterfaceTool(List<InterfaceToolEntity> interfaceToolList)
    {
        CommonResult res = new CommonResult();
        try {

            interfaceToolList.forEach(c -> {
                if (StringUtils.isBlank(c.getId())) {
                    c.setId(RandomGUID.generatorGUID());
                    int rc = interfaceToolMapper.insert(c);
                } else {

                    UpdateWrapper<InterfaceToolEntity> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id", c.getId());
                    interfaceToolMapper.update(c, updateWrapper);
                }
            });
        }
        catch (Exception e){
            res.setResult(false);
            res.setErrMsg(e.getMessage());
        }
        return res;
    }

    @Transactional
    public CommonResult saveInterfaceToolNoClob(List<InterfaceToolEntity> interfaceToolList)
    {
        CommonResult res = new CommonResult();
        try {

            interfaceToolList.forEach(c -> {
                if (StringUtils.isBlank(c.getId())) {
                    c.setId(RandomGUID.generatorGUID());
                    int rc = interfaceToolMapper.insert(c);
                } else {

                    UpdateWrapper<InterfaceToolEntity> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id", c.getId());
                    interfaceToolMapper.update(c, updateWrapper);
                }
            });
        }
        catch (Exception e){
            res.setResult(false);
            res.setErrMsg(e.getMessage());
        }
        return res;
    }
}
